/**
  ******************************************************************************
  * @file    main.c
  * @author  AE Team
  * @version 1.0.3
  * @date    2024-05-28
  * @brief   本样例演示UART1作为LIN的从机和主机进行通信
  *          主机发送随机的8字节数据给从机，紧接着读回上一帧发送的8字节数据
  *          通过UART2向PC打印LOG信息
  *          用户只需要根据自己的应用层协议实现以下三个函数即可：
  *          LIN_SlaveIT_PID_Process_CallBack   本例实现2个ID，一个用用于主机发送数据，一个用于主机读取数据
  *          LIN_SlaveIT_SendFrameOver_CallBack 本例发送完一帧数据后将UserRecvBuf和UserSendBuf内容清空
  *          LIN_SlaveIT_RecvFrameOver_CallBack 本例收到一帧数据之后将UserRecvBuf中的内容赋值给UserSendBuf
  *
  * @note    硬件平台：SYM32L010_StartKit_V1.0
  *          接线说明：短接J13
  *                    断开J12、J23、J19
  *
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; Copyright (c) 2024 SIYIMicro.
  * All rights reserved.</center></h2>
  *
  *
  ******************************************************************************
  */


/******************************************************************************/
/* Include files                                                              */
/******************************************************************************/
#include "main.h"
#include "sym32l010_hal_sysctrl.h"
#include "sym32l010_hal_systick.h"
#include "sym32l010_hal_flash.h"
#include "sym32l010_hal_gpio.h"
#include "sym32l010_hal_uart.h"
#include "sym32l010_startkit_board.h"

#include "lin.h"
#include "stdlib.h"

/******************************************************************************/
/* Local pre-processor symbols/macros ('#define')                             */
/******************************************************************************/
#define UART_LIN    SYM_UART1
#define UART_PRINT  SYM_UART2

/******************************************************************************/
/* Global variable definitions (declared in header file with 'extern')        */
/******************************************************************************/
UART_HandleTypeDef hUart;

/******************************************************************************/
/* Local type definitions ('typedef')                                         */
/******************************************************************************/

/******************************************************************************/
/* Local function prototypes ('static')                                       */
/******************************************************************************/
static void LIN_Port_Init(void);

/******************************************************************************/
/* Local variable definitions ('static')                                      */
/******************************************************************************/

/******************************************************************************/
/* Local pre-processor symbols/macros ('#define')                             */
/******************************************************************************/

/******************************************************************************/
/* Function implementation - global ('extern') and local ('static')           */
/******************************************************************************/


/*******************************************************************************
 ** \brief  Main function of project
 **
 ** \return uint32_t return value, if needed
 **
 **
 ******************************************************************************/
int32_t main(void)
{
    MemClr(&hUart, sizeof(hUart));

    // 使能外设时钟
    HAL_SYSCTRL_UART1_CLK_ENABLE();                                   // UART1作为LIN传输
    HAL_SYSCTRL_UART2_CLK_ENABLE();                                   // UART2作为UART向PC打印Log

    // 配置HSI时钟
    HAL_SysCtrl_HSI_SetDiv(HSIOSC_TO_HSI8MHZ);

    // 初始化SysTick
    HAL_InitTick(8000000);

    // Print(UART2) Port And LIN(UART1) Port Init.
    Bsp_Uart_Init(8000000, 115200);
    printf("Print(Uart2) Init.\r\n");

    LIN_Port_Init();

    // Init UART_LIN As LIN Mode
    hUart.Instance = UART_LIN;
    hUart.Init.PclkFreq = 8000000;
    hUart.Init.BaudRate = 20000;
    HAL_UART_Init(&hUart);
    HAL_UART_TXD_Enable(UART_LIN);
    HAL_UART_RXD_Enable(UART_LIN);
    HAL_LIN_Enable(UART_LIN, LIN_BRKLEN_11BIT);

    HAL_UART_CLR_FLAG(&hUart, UART_IT_FLAG_ALL);
    HAL_UART_DISABLE_IT(&hUart, UART_IT_SOURCE_RC);
    HAL_UART_ENABLE_IT(&hUart, UART_IT_SOURCE_RXBRK);                 // 开启Lin间隔段接收完成标志

    NVIC_ClearPendingIRQ(UART1_IRQn);
    NVIC_EnableIRQ(UART1_IRQn);
    printf("LIN(Uart1) Slave Init\r\n");

    while (1)
    {
        ;
    }
}


/*******************************************************************************
 * @brief  LIN 端口初始化
 * @param  None
 * @retval None
 * @note
 */
static void LIN_Port_Init(void)
{
    GPIO_InitTypeDef GPIO_InitStruct;

    MemClr(&GPIO_InitStruct, sizeof(GPIO_InitStruct));

    HAL_SYSCTRL_GPIOA_CLK_ENABLE();

    // Config PA06 AS UART1.TXD <--> LIN
    AFx_UART1TXD_PA06();
    GPIO_InitStruct.Pins = GPIO_PIN_6;
    GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
    HAL_GPIO_Init(SYM_GPIOA, &GPIO_InitStruct);

    // Config PA05 AS UART1.RXD <--> LIN
    AFx_UART1RXD_PA05();
    GPIO_InitStruct.Pins = GPIO_PIN_5;
    GPIO_InitStruct.Pull = GPIO_PULL_UP;
    GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
    HAL_GPIO_Init(SYM_GPIOA, &GPIO_InitStruct);
}


/*******************************************************************************
 ** EOF (not truncated)
 ******************************************************************************/
#ifdef  USE_FULL_ASSERT
/**
  * @brief  Reports the name of the source file and the source line number
  *         where the assert_param error has occurred.
  * @param  file: pointer to the source file name
  * @param  line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t *file, uint32_t line)
{
    /* USER CODE BEGIN */
    /* User can add his own implementation to report the file name and line number,
       tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
    /* USER CODE END */
}
#endif /* USE_FULL_ASSERT */


/************************ (C) COPYRIGHT SIYIMicro *****END OF FILE*************/
